var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var listdata = mm("do_ListData");
var couponUrl = require("url/coupon");
var qqHttp = require("helper/http");
var communityUtil = require("helper/community");

// andriod返回键
page.on("back", function() {
	app.closePage();
});
/*头部*/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
ui(addheader+".labelTitle").text="代金券兑换";
ui(addheader+".headerLeft").on("touch",function() {
	app.closePage()
});
// 页面装载完成后，开始初始化工作
page.on("loaded", function() {
	binddata();
});
var page;
var aLayoutRoot = ui("aLayoutRoot");
var listView = ui("listView");
listView.bindItems(listdata);

function binddata() {
	page = 1;
	var params = {
		page : page,
		communityId : communityUtil.communityVisitId()
	}
	qqHttp.post(couponUrl.url.base.page, params, function(data) {
		listView.rebound();
		listdata.removeAll();// 清除数据
		if (data.page) {
			if(data.page.content.length){
				listdata.addData(data.page.content);
			}else{
				listdata.addData(noData);
			}
			listView.refreshItems();// 刷新item数据
		}
	});
}
function getNextPageData() {
		page++;
		var params = {
			page : page,
			communityId : communityUtil.communityVisitId()
		}
		qqHttp.post(couponUrl.url.base.page, params, function(data) {
			if (data.page) {
				listView.rebound();
				listdata.addData(data.page.content);
				listView.refreshItems();
			}
		});
}
// 下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		listView.rebound();
		binddata();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (data.state == 2) {
		listView.rebound();
		getNextPageData();
	}
});
